<?php

namespace App\Http\Controllers;

use App\Http\Response\OKResponse;
use App\Models\GiftLessonStudent;
use App\Models\GiftLessonShare;
use App\Exceptions\GiftLessonStudentNotFountException;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Laravel\Lumen\Routing\Controller as BaseController;

class AdminGiftLessonShareController extends BaseController
{
    //

    public function retrieve(Request $request)
    {
        $this->validate($request, [
            "studentId"      => "integer",
            "status"         => "integer",
            "pageSize"       => "integer|max:100|min:5",
            "page"           => "integer|min:1",
        ]);

        $query = GiftLessonShare::query();
        if ($request->has("studentId")) {
            $query->where("student_id", $request->input("studentId"));
        }
        if ($request->has("status")) {
            $query->where("status", $request->input("status"));
        }
        $total = $query->count();
        $page = intval($request->input("page", 1));
        $pageSize = intval($request->input("pageSize", 10));
        $shares = $query->skip($pageSize * ($page - 1))->take($pageSize)->get();
        $result = [];
        $acceptance = null;
        foreach ($shares as $item) {
            $acceptanceFirst = $item->acceptances()->first();
            if (!empty($acceptanceFirst)) {
                $acceptance = [
                    'studentId' => $acceptanceFirst->getStudentId(),
                    'status' => $acceptanceFirst->getStatus(),
                    'expiredAt' => $acceptanceFirst->getExpiredAt()->getTimestamp(),
                    'giftedAt' => empty($acceptanceFirst->getGiftedAt()) ? null : $acceptanceFirst->getGiftedAt()->getTimestamp()
                ];
            }

            $result[] = [
                'studentId'   => $item->getStudentId(),
                'code'        => $item->getShareCode(),
                'lessonCount' => $item->getLessonCount(),
                'expiredAt'   => $item->getExpiredAt()->getTimestamp(),
                'status'      => $item->getStatus(),
                'acceptance'  => $acceptance,
            ];
        }

        return new JsonResponse([
            "total"    => $total,
            "page"     => $page,
            "pageSize" => $pageSize,
            "result"   => $result,
        ]);
    }

    public function retrieveOne(Request $request, $studentId)
    {
        $giftLessonStudent = GiftLessonStudent::find($studentId);
        if (empty($giftLessonStudent)) {
            throw new GiftLessonStudentNotFountException();
        }

        $result = [
            'shareCount' => $giftLessonStudent['share_count'],
            'acceptedCount' => $giftLessonStudent['accepted_count'],
            'totalGift' => $giftLessonStudent['total_gift'],
            'status' => $giftLessonStudent['status']
        ];

        return new JsonResponse($result);
    }

    public function update(Request $request, $studentId)
    {
        $request->merge([
            'student_id' => $studentId,
        ]);
        $this->validate($request, [
            'student_id' => 'required|integer',
            'status' => 'required|integer|in:0,1'
        ]);
        $body = [
            'student_id' => intval($request->input('student_id'))
        ];
        $giftLessonStudent = GiftLessonStudent::firstOrCreate($body);
        $giftLessonStudent->setStatus($request->input('status'));
        $giftLessonStudent->save();

        return new OKResponse();
    }
}
